/**
 * Window that splits its area into multiple parts.
 * 
 * @author Andrey Anisimov <hercules@inbox.ru>
 */

import core.ui.Window;
import core.ui.CompositeWindow;

class SplitWindow extends CompositeWindow {
	
	/**
	 * Creates new split window.
	 * 
	 * @param parent (Window) [optional] parent window
	 * @param x, y (int) [optional] coordinates
	 * @param width, height (int) [optional] window size.
	 */		
	this.SplitWindow = function(parent, x, y, width, height) {
		super(parent, x, y, width, height);
		this.panes = {};
		this.updateLayout();
	}

	/**
	 * Adds the new child pane.
	 * Note: the splitType parameter is a bitwise combination of the following:
	 * SplitWindow.SPLIT_LEFT		- insert new pane to the left
	 * SplitWindow.SPLIT_RIGHT		- insert new pane to the right
	 * SplitWindow.SPLIT_TOP		- insert new pane to the top
	 * SplitWindow.SPLIT_BOTTOM	- insert new pane to the bottom
	 * SplitWindow.SPLIT_FILL		- fill the remaining space
	 * 
	 * @param pane (Window) pane to add (should be a child window of SplitWindow)
	 * @param splitType (int) splitting type
	 */
	this.addPane = function(wnd, splitType) {
		wnd.setStyle('position', 'absolute');
		wnd.setStyle('float', 'none');
		this.adjustPane(wnd, splitType);
		this.panes[wnd.getId()] = splitType;
	}
	
	/**
	 * @protected
	 * Adjusts specified pane position.
	 *
	 * @param pane (Window) child pane
	 * @param splitType (int) pane splitting type
	 */
	this.adjustPane = function(wnd, splitType) {
		var x = this.splitLeft;
		var y = this.splitTop;
		if (splitType) {
			if (splitType & SplitWindow.SPLIT_LEFT) {
				this.splitLeft += wnd.getWidth();
			}
			if (splitType & SplitWindow.SPLIT_TOP) {
				this.splitTop += wnd.getHeight();
			}
			if (splitType & SplitWindow.SPLIT_RIGHT) {
				this.splitRight -= wnd.getWidth();
				x = this.splitRight;
			}
			if (splitType & SplitWindow.SPLIT_BOTTOM) {
				this.splitBottom -= wnd.getHeight();
				y = this.splitBottom;
			}
			if (splitType & SplitWindow.SPLIT_FILL) {
				// Fill all remaining space
				wnd.resize(Math.abs(this.splitRight - this.splitLeft),
					Math.abs(this.splitBottom - this.splitTop));
			}
		}
		wnd.move(x, y);
	}
	
	/**
	 * @protected
	 * Updates child panes size and position.
	 */
	this.updateLayout = function() {
		this.splitLeft = 0;
		this.splitTop = 0;
		this.splitRight = this.getClientWidth();
		this.splitBottom = this.getClientHeight();
		for (var id in this.panes) {
			var pane = Window.forId(id);
			var splitType = this.panes[id];
			this.adjustPane(pane, splitType);
		}
	}
}

core.ui.SplitWindow.SPLIT_LEFT = 0x0001;
core.ui.SplitWindow.SPLIT_RIGHT = 0x0004;
core.ui.SplitWindow.SPLIT_TOP = 0x0008;
core.ui.SplitWindow.SPLIT_BOTTOM = 0x0010;
core.ui.SplitWindow.SPLIT_FILL = 0x0040;

